/*
 * Copyright (c) sl All rights reserved.
 * 本软件源代码版权归sl所有,未经许可不得任意复制与传播.
 */
package com.sl.crawler.manager;

import com.sl.crawler.common.enums.EmYn;
import com.sl.crawler.common.utils.DateUtils;
import com.sl.crawler.dao.XiaoquDetailDayDao;
import com.sl.crawler.dao.base.BaseDao;
import com.sl.crawler.domain.Xiaoqu;
import com.sl.crawler.domain.XiaoquDetailDay;
import com.sl.crawler.manager.base.BaseManager;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author sl
 */
@Service
public class XiaoquDetailDayManager extends BaseManager<XiaoquDetailDay, Long> {
    @Autowired
    private XiaoquDetailDayDao xiaoquDetailDayDao;

    public BaseDao<XiaoquDetailDay, Long> getDao() {
        return xiaoquDetailDayDao;
    }
    //自定义扩展

    public void saveOrUpdateXiaoquDetailDay(Xiaoqu xiaoqu) {
        XiaoquDetailDay xiaoquDetailDay = buildXiaoquDetailDay(xiaoqu);

        XiaoquDetailDay xiaoquDetailDayParam = new XiaoquDetailDay();
        xiaoquDetailDayParam.setXiaoquId(xiaoqu.getXiaoquId());
        xiaoquDetailDayParam.setYn(EmYn.YES.value());
        xiaoquDetailDayParam.setCrawlerDate(DateUtils.format(DateUtils.yyyy_MM_dd));
        XiaoquDetailDay xiaoquDetailDayDb = this.selectOneEntry(xiaoquDetailDayParam);
        if (xiaoquDetailDayDb == null || xiaoquDetailDayDb.getId() == null) {
            this.insertEntry(xiaoquDetailDay);
        } else {
            xiaoqu.setId(xiaoquDetailDayDb.getId());
            this.updateByKey(xiaoquDetailDay);
        }
    }

    private XiaoquDetailDay buildXiaoquDetailDay(Xiaoqu xiaoqu) {
        XiaoquDetailDay xiaoquDetailDay = new XiaoquDetailDay();
        try {
            BeanUtils.copyProperties(xiaoquDetailDay, xiaoqu);
        } catch (Exception e) {
            LOG.error("小区每日明细对象转换失败", e);
        }
        xiaoquDetailDay.setCrawlerDate(DateUtils.format(DateUtils.yyyy_MM_dd));
        return xiaoquDetailDay;
    }
}